package com.zuozuo.reflection;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class Demo6 {
    public static void main(String[] args) throws Exception {
        Class c = Class.forName("com.zuozuo.reflection.Date");
        Annotation[] annotations = c.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        table table = (com.zuozuo.reflection.table) c.getAnnotation(com.zuozuo.reflection.table.class);
        System.out.println(table.db_table());
        Field[] declaredFields = c.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            field annotation = declaredField.getAnnotation(field.class);
            System.out.println(annotation.name());
            System.out.println(annotation.type());
            System.out.println(annotation.lengh());
        }
    }
}

@table(db_table = "db_student")
class Date {
    @field(name = "id", type = "long", lengh = 10)
    private long id;

    @field(name = "name", type = "varchar", lengh = 10)
    private String name;

    @field(name = "age", type = "int", lengh = 10)
    private int age;

    public Date() {
    }

    public Date(long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Date{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface table {
    String db_table();
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface field {
    String name();

    String type();

    int lengh();
}
